#include "Util.hpp"
#include <functional>
#include <vector>
// #include <system_error>

using func_t = std::function<void()>;

template <typename alloc>
void INT(alloc &v)
{
    v.push_back(printLog);
    v.push_back(downLoad);
    v.push_back(execSQL);
}

template <typename alloc>
void execOther(alloc &cbs)
{
    for (const auto &cb : cbs)
    {
        cb();
    }
}

int main()
{
    std::vector<func_t> vbs;
    INT(vbs);
    setNonBlock(0);
    char buffer[1024];
    while (true)
    {
        std::cout << ">>>: " << std::flush;
        ssize_t n = read(0, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n - 1] = 0;
            std::cout << "echo #" << buffer << std::endl;
        }
        else if (n == 0)
        {
            std::cout << "read end....." << std::endl;
        }
        else
        {
            // std::system_error error(errno, std::generic_category());
            // std::cerr << "Error code: " << error.code().value() << " - " << error.what() << std::endl;
            if (EAGAIN == errno)
            {
                std::cout << "资源未就绪" << std::endl;
                execOther(vbs);
            }
            else if (EINTR == errno)
            {
                continue;
            }
        }
        
        sleep(1);
    }
    return 0;
}